<template>
	<view class="wrap">
		<view class="user-section">
			<image class="bg" src="/static/user-bg.png"></image>
			<view class="user-info-box">
				<view class="portrait-box">
					<image class="portrait" :src="userInfo.avatar_url || '/static/imgs/user.png'"></image>
				</view>
				<view class="info-box">
					<text class="username" v-if="isLogin">{{ userInfo.nickname || '--' }}</text>
					<text class="b-login" v-else @click="onRedirect('user-login', true)">立即登录</text>
				</view>
			</view>
		</view>
		<view class="cover-container">
			<view class="card-box" v-if="isLogin">
				<template v-if="userInfo.fund_state == 2">
					<view class="b-main">
						<view class="iconfont icon-fund-card"></view>
						<view class="s-cont">储值卡被屏蔽</view>
					</view>
				</template>
				<template v-else>
					<view class="b-main" @click="onRedirect('mine-fund')">
						<view class="iconfont icon-fund-card"></view>
						<view class="s-cont">储值卡余额：¥{{userInfo.fund_amount_left}}</view>
					</view>
					<view class="b-action" @click="onRedirect('mine-fund')">
						<view class="s-btn">立即充值</view>
					</view>
				</template>
			</view>
			<view class="history-section icon">
				<list-cell icon="icon-user-order" title="我的订单" :tips="(userInfo.count_order || 0)+''"
					@eventClick="onRedirect('order-list')">
				</list-cell>
				<list-cell icon="icon-like-yes" title="我的收藏" :tips="(userInfo.count_like || 0)+''"
					@eventClick="onRedirect('mine-like')">
				</list-cell>
				<list-cell icon="icon-user-info" title="个人资料" @eventClick="onRedirect('user-info')">
				</list-cell>
				<list-cell icon="icon-user-question" title="常见问题" @eventClick="onRedirect('question-list')">
				</list-cell>
				<list-cell icon="icon-user-about" title="关于" @eventClick="onRedirect('setting')">
				</list-cell>
			</view>
			<view class="btn-logout" @click="onLogout" v-if="isLogin">切换账户</view>
		</view>
	</view>
</template>

<script>
	import {
		getUserInfo,
		logout
	} from '@/service/api/user.js'
	import mixinsAuth from '@/mixins/auth.js'
	import mixinsCommon from '@/mixins/common.js'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				userInfo: {}
			}
		},
		onLoad() {
			if (this.isLogin) {
				getUserInfo().then(([err, res]) => {
					console.log('getUserInfo', err, res)
					if (!err) {
						this.userInfo = res
					}
				})
			}
		},
		methods: {
			onRedirect(type, muteLogin) {
				console.log('onRedirect', type, muteLogin)
				if (!muteLogin) {
					if (!this.checkLogin()) {
						return
					}
				}
				var pages = {
					'user-login': '/pages/user/login',
					'mine-fund': '/pages/user/fund',
					'mine-like': '/pages/user/like',
					'order-list': '/pages/order/list',
					'user-info': '/pages/user/info',
					'question-list': '/pages/content/question',
					'feedback': '/pages/user/feedback',
					'setting': '/pages/user/setting'
				}
				var page = pages[type] || ''
				if (page) {
					if (type === 'order-list') {
						uni.switchTab({
							url: page
						})
					} else {
						uni.navigateTo({
							url: page
						})
					}
				}
			},
			onLogout() {
				uni.showModal({
					content: '确定要切换账户么',
				}).then(([err, res]) => {
					if (!err) {
						if (res.confirm) {
							logout().then(([err, res]) => {
								console.log('logout', err, res)
								this.$store.commit('delAccessToken');
								uni.reLaunch({
									url: '/pages/user/login'
								})
							})
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.user-section {
		height: 400upx;
		padding: 120upx 30upx 0;
		position: relative;

		.bg {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			filter: blur(1px);
			opacity: 0.9;
		}
	}

	.user-info-box {
		height: 180upx;
		display: flex;
		align-items: center;
		position: relative;
		z-index: 1;

		.portrait {
			width: 130upx;
			height: 130upx;
			border: 5upx solid #fff;
			border-radius: 50%;
		}

		.username {
			margin-left: 20upx;
			font-size: 40upx;
			color: #fff;
		}

		.b-login {
			margin-left: 20upx;
			font-size: 40upx;
			color: #fff;
		}
	}

	.cover-container {
		margin-top: -140upx;
		padding: 0 30upx;
		position: relative;
		padding-bottom: 20upx;

		.arc {
			position: absolute;
			left: 0;
			top: -34upx;
			width: 100%;
			height: 36upx;
		}
	}

	.tj-sction {
		display: flex;
		justify-content: space-around;
		align-content: center;
		background: #fff;
		border-radius: 10upx;

		.tj-item {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			height: 140upx;
			font-size: $font-sm;
			color: #75787d;
		}

		.num {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 8upx;
		}
	}

	.card-box {
		padding: 30upx;
		background: #DA5650;
		border-radius: 10upx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.b-main {
			display: flex;
			align-items: center;

			.iconfont {
				margin-right: 20upx;
				font-size: 50upx;
				color: #fff;
			}

			.s-cont {
				font-size: 26upx;
				color: #fff;
			}
		}

		.b-action {
			.s-btn {
				padding: 0 30upx;
				background: #fff;
				height: 60upx;
				line-height: 60upx;
				border-radius: 30upx;
				font-size: 24upx;
				color: #333;
			}
		}
	}

	.history-section {
		margin-top: 50upx;
		background: #fff;
		border-radius: 10upx;
	}

	.btn-logout {
		margin-top: 60upx;
		height: 80upx;
		line-height: 80upx;
		text-align: center;
		color: #DA5650;
		background: #ffffff;
		font-size: 32upx;
		border: 1px solid #DA5650;
		border-radius: 40upx;
		letter-spacing: 10upx;
	}
</style>
